package com.junzhiit.nengji.api.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.api.service.CommissionHistoryService;
import com.junzhiit.nengji.api.service.IncomeHistoryService;
import com.junzhiit.nengji.api.service.SendPhoneService;
import com.junzhiit.nengji.api.service.UserConfigService;
import com.junzhiit.nengji.api.service.UserService;
import com.junzhiit.nengji.api.web.exception.LoginResponseNotFoundException;
import com.junzhiit.nengji.api.web.util.aliyun.SmsUtil;
import com.junzhiit.nengji.api.web.util.wechat.WeChatUtils;
import com.junzhiit.nengji.api.web.util.wechat.bean.LoginResponse;
import com.junzhiit.nengji.api.web.vo.LoginVO;
import com.junzhiit.nengji.api.web.vo.ResultVO;
import com.junzhiit.nengji.api.web.vo.UserEditVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

	@Autowired
	private UserService userService;
	
	@Autowired
	private UserConfigService userConfigService;
	
	@Autowired
	private SendPhoneService sendPhoneService;
	
	@Autowired
	private IncomeHistoryService incomeHistoryService;
	
	@Autowired
	private CommissionHistoryService commissionHistoryService;
	
	@Value("${upload.mall.user.avatarurl}")
	private String userAvatarurlPath;
	
	@ApiOperation(value="授权")
	@RequestMapping(value = "/authorization",method = RequestMethod.POST)
	public ResultVO authorization(LoginVO loginVO){
		ResultVO result = new ResultVO();
		AssertUtil.notBlank(loginVO.getLoginCode(), "LoginCode is required.");
		LoginResponse response = WeChatUtils.authorization(loginVO.getLoginCode());
		if(StringUtils.isEmpty(response.getOpenId())){
		 throw new LoginResponseNotFoundException("授权失败");
		}
		result.setData(userService.userAuth(loginVO, response));
		return result;
	}
	
	@ApiOperation(value = "查询用户信息ById")
	@RequestMapping(value = "/{id}",method = RequestMethod.GET)
	public ResultVO getUserById(@PathVariable String id){
		AssertUtil.notBlank(id, "id is required.");
		return userService.getUserById(id);
	}
	
	@ApiOperation(value = "查询用户信息ByCode")
	@RequestMapping(value = "/code/{userCode}",method = RequestMethod.GET)
	public ResultVO getUserByCode(@PathVariable String userCode){
		AssertUtil.notBlank(userCode, "userCode is required.");
		return userService.getUserByCode(userCode);
	}
	
	@ApiOperation(value = "我的人脉")
	@RequestMapping(value = "/relationships",method = RequestMethod.GET)
	public ResultVO getUserRelationships(String userId,Integer sortStatus,String param,Integer pageNumber,Integer maxPageRow){
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notNull(pageNumber, "pageNumber is required.");
		AssertUtil.notNull(maxPageRow, "maxPageRow is required.");
		return userService.getUserRelationships(userId,sortStatus,param,pageNumber,maxPageRow);
	}
	
	@ApiOperation(value = "我的提现")
	@RequestMapping(value = "/bring/cash",method = RequestMethod.GET)
	public ResultVO takeCash(String userId){
		AssertUtil.notBlank(userId, "userId is required.");
		return userService.takeCash(userId);
	}
	
	@ApiOperation(value = "修改用户个人信息")
	@RequestMapping(method = RequestMethod.PUT)
	public ResultVO edit(UserEditVO userEditVO) {
		AssertUtil.notBlank(userEditVO.getId(), "id is required.");
		return userService.edit(userEditVO);
	}
	
	@ApiOperation(value = "发送邮箱验证码")
	@RequestMapping(value = "/send/email",method = RequestMethod.POST)
	public ResultVO sendEmailCode(String userId,String email) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(email, "email is required.");
		return userService.sendEmailCode(userId,email);
	}
	
	@ApiOperation(value = "绑定邮箱/修改绑定邮箱")
	@RequestMapping(value = "/bind/email",method = RequestMethod.PUT)
	public ResultVO bindEmail(String userId,String email,String code) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(email, "email is required.");
		AssertUtil.notBlank(code, "code is required.");
		return userService.bindEmail(userId,email,code);
	}
	
	@ApiOperation(value = "发送手机验证码")
	@RequestMapping(value = "/send/phone",method = RequestMethod.POST)
	public ResultVO sendMobilePhoneCode(String userId,String mobilePhone) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(mobilePhone, "mobilePhone is required.");
		return sendPhoneService.sendPhoneCodeByUserId(userId, mobilePhone, SmsUtil.ALIYUN_SMS_CHECKED_CODE);
	}
	
	@ApiOperation(value = "发送手机验证码不需要userId")
	@RequestMapping(value = "/send/phone/code",method = RequestMethod.POST)
	public ResultVO sendMobilePhoneCode(String mobilePhone) {
		AssertUtil.notBlank(mobilePhone, "mobilePhone is required.");
		return sendPhoneService.sendPhoneCode(mobilePhone, SmsUtil.ALIYUN_SMS_CHECKED_CODE);
	}
	
	@ApiOperation(value = "绑定手机号码/修改绑定手机号码")
	@RequestMapping(value = "/bind/phone",method = RequestMethod.PUT)
	public ResultVO bindMobilePhone(String userId,String mobilePhone,String code,String name) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(mobilePhone, "mobilePhone is required.");
		AssertUtil.notBlank(code, "code is required.");
		return userService.bindMobilePhone(userId,mobilePhone,code,name);
	}
	
	@ApiOperation(value = "邮箱验证-发送验证码")
	@RequestMapping(value = "/email/check/send",method = RequestMethod.POST)
	public ResultVO emailCheckSendCode(String userId,String email) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(email, "email is required.");
		return userService.emailCheckSendCode(userId,email);
	}
	
	@ApiOperation(value = "邮箱验证")
	@RequestMapping(value = "/email/check",method = RequestMethod.PUT)
	public ResultVO emailCheck(String userId,String email,String code) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(email, "email is required.");
		AssertUtil.notBlank(code, "code is required.");
		return userService.emailCheck(userId,email,code);
	}
	
	@ApiOperation(value = "手机号码验证-发送短信验证码")
	@RequestMapping(value = "/phone/check/send",method = RequestMethod.POST)
	public ResultVO mobilePhoneCheckSendCode(String userId) {
		AssertUtil.notBlank(userId, "userId is required.");
		return sendPhoneService.sendPhoneCodeByUserId(userId, SmsUtil.ALIYUN_SMS_CHECKED_CODE);
	}
	
	@ApiOperation(value = "手机号码验证")
	@RequestMapping(value = "/phone/check",method = RequestMethod.PUT)
	public ResultVO mobilePhoneCheck(String userId,String code) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(code, "code is required.");
		return userService.mobilePhoneCheck(userId,code);
	}
	
/*	@ApiOperation(value = "更新用户二维码(勿用)")
	@RequestMapping(value = "/update/qrcode/list",method = RequestMethod.PUT)
	public ResultVO updateUserQrcode() {
		return userConfigService.updateQrcode();
	}*/
	
/*	@ApiOperation(value = "更新个人用户二维码(勿用)")
	@RequestMapping(value = "/update/qrcode",method = RequestMethod.PUT)
	public ResultVO updateUserQrcodeById(String userId) {
		return userService.updateUserQrcodeById(userId);
	}*/
	
	/*@ApiOperation(value = "更新排队返利(勿用)")
	@RequestMapping(value = "/update/rebate",method = RequestMethod.GET)
	public ResultVO updateAllRebate() {
		return rebateService.updateAllRebate();
	}*/
	
	/*@ApiOperation(value = "更新单个用户二维码(勿用)")
	@RequestMapping(value = "/update/user/qrcode",method = RequestMethod.GET)
	public UserConfig updateQrcodeByUserId(String userIds) {
		return  userConfigService.qrcode(userIds,null);
	}*/
	
	@ApiOperation(value = "查询用户流水")
	@RequestMapping(value = "/income",method = RequestMethod.GET)
	public ResultVO getIncomeHistory(String userId,String date,Integer pageNumber, Integer maxPageRow) {
		return  incomeHistoryService.getIncomeHistory(userId,date,pageNumber, maxPageRow);
	}
	
	@ApiOperation(value = "查询用户分销明细")
	@RequestMapping(value = "/commission",method = RequestMethod.GET)
	public ResultVO getCommissionHistory(String userId,String startTime,String endTime,Integer pageNumber, Integer maxPageRow) {
		return  commissionHistoryService.countCommissionHistory(userId, startTime, endTime, pageNumber, maxPageRow);
	}
	
/*	@ApiOperation(value="替换用户二维码")
	@RequestMapping(value = "/test",method = RequestMethod.POST)
	public ResultVO test(){
		userConfigService.getAvatarur();	
		ResultVO result = new ResultVO();
		User user = new User();
		List<User> list = userDAO.findByUser(user);
		for(User params : list){
			//userConfigService.qrcode(params.getId(), params.getAvatarUrl());
			try {
				HttpClientUtils.imageDownloader(params.getAvatarUrl(),userAvatarurlPath+params.getId()+".png");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}*/
	@ApiOperation(value = "在线充值")
	@RequestMapping(value = "/online/recharge",method = RequestMethod.POST)
	public ResultVO onlineRecharge(HttpServletRequest request,String userId,Double money,String tradeType){
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(tradeType, "tradeType is required.");
		AssertUtil.notNull(money, "money is required.");
		return userConfigService.onlineRecharge(request, userId, money, tradeType);
	}
}
